package com.viish.apps.tripletriad.carte;

import java.io.IOException;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.viish.apps.tripletriad.R;
import com.viish.apps.tripletriad.views.MinimalistCardView;

public class MyDecks extends ListActivity implements OnClickListener
{
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getListView().setBackgroundResource(R.drawable.background_landscape);
        getListView().setPadding(5, 2, 5, 2);
		
		DatabaseStream dbs = new DatabaseStream(this);
		ArrayList<String> decksNames = dbs.getDecks();
		ArrayList<Deck> decks = new ArrayList<Deck>();
		
		for (String deck : decksNames)
		{
			decks.add(new Deck(deck, dbs.getDeck(deck)));
		}
		dbs.close();
		this.setListAdapter(new Adapter(this, R.layout.row, decks));
	}

	@Override
	public void onClick(View v) 
	{
		final String deckName = (String) v.getTag(R.id.name);
		final Context context = this;
		
		if (v.getTag(R.id.bMyDecks).equals("Delete"))
		{
			DatabaseStream dbs = new DatabaseStream(this);
			dbs.deleteDeck(deckName);
			
			ArrayList<String> decksNames = dbs.getDecks();
			ArrayList<Deck> decks = new ArrayList<Deck>();
			
			for (String deck : decksNames)
			{
				decks.add(new Deck(deck, dbs.getDeck(deck)));
			}
			
			this.setListAdapter(new Adapter(this, R.layout.row, decks));
			
			dbs.close();
		}
		else if (v.getTag(R.id.bMyDecks).equals("Edit"))
		{
			AlertDialog.Builder alert = new AlertDialog.Builder(this);  

			alert.setTitle(getString(R.string.enter_deck_name));
			alert.setIcon(R.drawable.pampa);
			 
			final EditText deckNameET = new EditText(this);
			deckNameET.setText(deckName);
			alert.setView(deckNameET);
			
			alert.setNegativeButton(getString(R.string.button_back), null);
			alert.setNeutralButton(getString(R.string.menu_rename), new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface arg0, int arg1) 
				{
					DatabaseStream dbs = new DatabaseStream(context);
					String newDeckName = deckNameET.getText().toString();
					
					if (newDeckName != "")
					{
						if (!dbs.isDeckNameAlreadyUsed(newDeckName)) dbs.renameDeck(deckName, newDeckName);
						else Toast.makeText(context, getString(R.string.deck_name_already_used), Toast.LENGTH_LONG).show();
						
						ArrayList<String> decksNames = dbs.getDecks();
						ArrayList<Deck> decks = new ArrayList<Deck>();
						
						for (String deck : decksNames)
						{
							decks.add(new Deck(deck, dbs.getDeck(deck)));
						}
						
						((ListActivity) context).setListAdapter(new Adapter(context, R.layout.row, decks));
					}
					dbs.close();
				}
			});
			
			alert.show();
		}
	}
}

class Adapter extends ArrayAdapter<Deck>
{
	private ArrayList<Deck> decks;
	private MyDecks context;
	
	public Adapter(Context context, int textViewResourceId, ArrayList<Deck> decks) 
	{
		super(context, textViewResourceId, decks);
		this.decks = decks;
		this.context = (MyDecks) context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        
        ArrayList<Card> cards = decks.get(position).getCards();
        String deckName = decks.get(position).getName();
        if (deckName != null && cards != null) 
        {
        	LinearLayout cardsLL = (LinearLayout) v.findViewById(R.id.cardViewsLL);
        	for (int i = 0; i < 5; i++)
        	{
        		try 
        		{
					Bitmap bm = BitmapFactory.decodeStream(v.getResources().getAssets().open(cards.get(i).getFullName() + ".1.png"));
					MinimalistCardView mcv = new MinimalistCardView(context, bm, cards.get(i), false);
					cardsLL.addView(mcv);					
				} 
        		catch (IOException e) 
        		{
					e.printStackTrace();
					Log.i("Error", e.toString());
				}
        	}
        	TextView tt = (TextView) v.findViewById(R.id.name);
        	Button delete = (Button) v.findViewById(R.id.bDelete);
        	Button edit = (Button) v.findViewById(R.id.bEdit);
        	
        	if (tt != null) 
        	{
        		tt.setText(deckName);
        		tt.setTextSize(15);
        		tt.setTextColor(Color.RED);
            }
        	if (delete != null)
        	{
        		delete.setTag(R.id.bMyDecks, "Delete");
        		delete.setTag(R.id.name, deckName);
        		delete.setOnClickListener(context);
        	}
        	if (edit != null)
        	{
        		edit.setTag(R.id.bMyDecks, "Edit");
        		edit.setTag(R.id.name, deckName);
        		edit.setOnClickListener(context);
        	}
        }
        return v;
	}
}
